<?php
/**
 * Created by PhpStorm.
 * User: holy
 * Date: 2017/11/7
 * Time: 上午10:47
 */

namespace Admin\Controller;
class ProductController extends CommonController
{
    function __construct()
    {
        parent::__construct();
        $this->categories = $this->all_categories();
    }


    public function all_categories()
    {
        $Category = M('Category');
        $categories = $Category->where("parent_id = 0")->select();
        foreach ($categories as $key => $value) {
            $children = $Category->where("parent_id = $value[id]")->select();
            $categories[$key]['children'] = $children;
        }
        return $categories;
    }

    /***
     * 查出所有商品
     * @return mixed
     */
    public function all_products()
    {
        $Category = M('Category');
        $products = $this->product->where("status = 1")->order('sort_order asc')->select();
        foreach ($products as $key => $value) {
            $category_id = $value['category_id'];
            $category = $Category->find($category_id);
            $products[$key]['category'] = $category;
        }
        return $products;
    }


    /***
     * 商品管理首页
     */
    public function index()
    {
        $Product = D('Product');
        $name = I('get.name');
        $category_id = I('get.category_id');
        //分页
        $count = $this->product->where("status = 1")->count();// 查询满足要求的总记录数
        $Page = new \Common\Util\Page($count, 3);// 实例化分页类 传入总记录数和每页显示的记录数
        $pages = $Page->show();// 分页显示输出
        //模糊搜索
        $map = [];
        if (isset($name)) {
            $map['name'] = array('like', "%" . $name . "%"); //按商品名称
        }
        if (isset($category_id)) {
            $map['category_id'] = array('like', "%" . $category_id . "%");
        }
        $this->products = $Product->where($map)->relation(true)->where("status = 1")->order('sort_order desc')->limit($Page->firstRow . ',' . $Page->listRows)->all_products();
//        dump($products); exit;
        $datas = array('is_top','is_new','is_hot','is_recommend','is_onsale',);
//        dump($datas);exit;
//        $this->assign('datas',$datas);
        $this->assign(compact('products','pages','datas'));
//        $this->assign('count', $count); //输出总记录数
//        $this->assign('page', $show);// 赋值分页输出
        $this->display();
    }


    /***
     * 新增商品
     */
    public function add()
    {
        $Product = D('Product');
        if (IS_POST) {
            if (!$Product->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($Product->getError());
            } else {
                // 验证通过 可以进行其他数据操作
                $Product->create();
                $id = $Product->add();
                if ($id) {
                    $this->success('新增商品成功', U('index'));
                }
                $galleries = I('post.gallery');
                foreach ($galleries as $v) {
                    $gallery['img'] = $v;
                    $gallery['product_id'] = $id;
                    $this->gallery->where("product_id = '$id'")->add($gallery);
                }
            }
        } else {
//            $bucket = 'hdjzq';
//            $accessKey = 'sDCa_uqbZSmgonwalGQMXJaCeU4Y5OBzYL-b09Ok';
//            $secretKey = 'Js0hw0oq0B_rTlCMdSwffZ_BINFtBexjng8iCaOK';
//            $auth = new Auth($accessKey, $secretKey);
//            $this->upToken = $auth->uploadToken($bucket);
//            echo $upToken;
            $this->display();
        }
    }

    /***
     * 编辑商品
     */
    public function edit()
    {
        $Product = D('Product');
        $id = I('get.id');
//        dump($id);exit;
        if (IS_POST) {
            if (!$Product->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($Product->getError());
            } else {
                // 验证通过 可以进行其他数据操作
                $Product->create();
                $galleries = I('post.gallery');
                $result = $this->gallery->where("product_id = '$id'")->select();
                if ($result) {
                    //有图片，更新相册
                    foreach ($galleries as $v) {
                        $gallery['img'] = $v;
                        $gallery['product_id'] = $id;
                        $this->gallery->where("product_id = '$id'")->save($gallery);
                    }
                } else {
                    //没有图片，就新增相册
                    foreach ($galleries as $v) {
                        $gallery['img'] = $v;
                        $gallery['product_id'] = $id;
                        $this->gallery->add($gallery);
                    }
                }
                $Product->where("id = '$id'")->save();
                $this->success('编辑商品成功', U('index'));
            }
        } else {
            //查出当前要编辑信息
            $product = $this->product->find($id);
            //查出当前要编辑的商品的相册
            $galleries = $this->gallery->where("product_id = '$id'")->select();

            $this->assign(compact('product', 'galleries'));
            $this->display();
        }
    }

    /***
     * AJAX排序
     */
    public function sort_all()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $sort_order = I('post.sort_order');
            $this->product->where("id = '$id'")->setField('sort_order', $sort_order);
        }
    }
    /***
     * ajax修改商品库存
     */
    public function stock_ajax()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $stock = I('post.stock');
            $this->product->where("id='$id'")->setField('stock', $stock);
        }
    }
    /***
     * ajax修改商品单价
     */
    public function price_ajax()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $price = I('post.price');
            $this->product->where("id='$id'")->setField('price', $price);
        }
    }


    /***
     * ajax删除商品相册
     */
    public function delete_gallery()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $this->gallery->delete($id);
        }
    }

    /***
     * ajax多选商品移至回收站
     */
    public function delete_all()
    {
        if (IS_AJAX) {
            $ids = I('post.checked_id');
            $map['id'] = array('in', $ids);
            $result = $this->product->where($map)->setField('status', 0);
            if ($result) {
                $info = array('status' => 1, 'msg' => '此商品已被移至回收站');
                $this->ajaxReturn($info);
            }
        }
    }

    /***
     * 商品放入回收站
     */
    public function cyble()
    {
        $Product = M('Product');
        $id = I('get.id');
        $Product->where("id ='$id'")->setField('status', 0);
        $this->success('商品放入回收站成功', U('Product/trash'));
    }


    /***
     * 加载商品回收站首页
     */
    public function trash()
    {
        $Product = D('Product');
        //分页
        $count = $this->product->where("status = 0")->count();// 查询满足要求的总记录数
        $Page = new \Common\Util\Page($count, 2);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        $this->products = $Product->where("status = 0")->relation(true)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('count', $count); //输出总记录数
        $this->assign('page', $show);// 赋值分页输出
        $this->display();
    }

    public function recyble()
    {
        $id = I('get.id');
        $this->product->where("id ='$id'")->setField('status', 1);
        $this->success('商品还原成功', U('Product/index'));
    }

    /***
     * 回收站商品彻底删除
     */
    public function delete_confirm()
    {
        $id = I('get.id');
//        dump($id);exit;
//        echo(123123);exit;
        $this->product->where("id = '$id'")->delete();
        $this->success('商品彻底删除成功', U('index'));
    }


    /***
     * ajax改变属性
     */
    public function change_attr()
    {
        if (IS_AJAX) {
//            dump($_POST);exit;
            $id = I('post.id');
            $attr = I('post.attr');
            $product = $this->product->find($id);
            $this->product->where("id = '$id'")->setField($attr,!$product[$attr]);
        }
    }

//    /***
//     *加载分类里面商品详情
//     */
//    public function detail(){
//        $id = I('get.id');
//        $Product = D('Product');
//        $datas = array('is_top','is_new','is_hot','is_recommend','is_onsale',);
//        $products = $Product->relation(true)->order('sort_order desc')->where("category_id = '$id'")->select();
//        $this->assign(compact('products','pages','datas'));
//        $this->display();
//    }
}

